package prefuse.treeview;




import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSplitPane;


import prefuse.Display;
import prefuse.data.Table;
import prefuse.gui.InterfaceHandler;
import prefuse.table.TableCreater;
import prefuse.vensim.VensimException;

 
/**
 *
 * @web http://java-buddy.blogspot.com/
 */
public class StartUp {
//	private VensimHelp vensimController;
	private String xmlFileLocation;
//	private String modelLocation;
//	private String vensimLib;
	private String label;
	private JFrame frame;
	private JSplitPane jSplitHorizontalPanel, jSplitVerticalPanel;
	private  JPanel jRightUpPanel;
	private   JFrameWin colorPanel ;
	private Double[] timeList ;
	private Table table;
//	private JComboBox combo;
	private JComponent display;
	private  JComponent treeview;
	private String selected = "Birth of Mosquito Larvae";
	private Set<Double> NodesTimeSet = new HashSet<Double>();
	private  Box box = new Box(BoxLayout.Y_AXIS);
	private TableCreater tableCtrl;
	private String[] varNames;
	private InterfaceHandler interfaceHandler;
	
	public StartUp(InterfaceHandler intfaceHandler) throws Exception{
		interfaceHandler = intfaceHandler;
		xmlFileLocation = interfaceHandler.getXmlLocation();
	//	xmlFileLocation = "/WNVExampleTree2Week.xml";
	//	modelLocation = "C:\\tmp\\WNVDecisionTree.vpm";
	//	vensimLib = "VenDLL32";
		label = "name";	   	
    	frame = new JFrame("DecisionTreeView");
        jRightUpPanel = new JPanel();
        colorPanel = new JFrameWin();
        treeview = TreeViewByInformation.demo(xmlFileLocation, label, colorPanel,interfaceHandler);        
   //     vensimController = new VensimHelp(vensimLib,modelLocation);
        NodesTimeSet = TreeViewByInformation.getTimeListSet();
        tableCtrl = new TableCreater(interfaceHandler);
        varNames = interfaceHandler.getVensimVarNamesbyArray();    
        init();
	}
	
	public void init(){
		try {
			
			Table tempT = createTable(selected);
			JComponent tempCom = creatDisplay(tempT);
			
			fillComboBox(tempCom);
			fillSplitVerticalPanel();
			fillSplitHorizontalPanel();
			fillFrame();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	
	public Table createTable(String varName){

//		Set<Double> timeList =  new HashSet<Double>();
//		timeList.add(0.0);
//		timeList.add(1.0);
//		timeList.add(2.0);
//		timeList.add(3.0);
//		timeList.add(4.0);
		 System.out.println("Selected varName  = " + varName);
		  try {
		//	table = tableCtrl.generateTable("C:\\tmp\\Test.txt",varName,NodesTimeSet);
			
			table = tableCtrl.generateTable("C:\\tmp\\Test.txt",varName,NodesTimeSet); //create a null table at the begin
			
	//		table = null;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		  return table;
	}
	
	
	public JComponent creatDisplay(Table t){
		display = tableCtrl.createVisualization(t);
		
		return display;
	}
	public void fillComboBox(JComponent displayTable) throws VensimException{
//
     	 box.add(TableCreater.getEncodingToolbar(tableCtrl,varNames));
     	 box.add(displayTable);
		
		
         jRightUpPanel.add(box);
//         combo.setEditable(false);
//         selected = (String) combo.getSelectedItem();
	}
	
//	public void comboBoxUpdate(){
//         combo.addActionListener(new ActionListener() {
//             public void actionPerformed(ActionEvent event) {
//              
//                 JComboBox comboBox = (JComboBox) event.getSource();
//                 String newSelected = (String) comboBox.getSelectedItem();
//                 try {
//                	// TableCreater.axisUpdateAction(tableCtrl, combo, "y", "ylab");
//                	 
//                	   System.out.println("Selected Item  = " + newSelected);
//				} catch (Exception e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//					
//				}
//                 System.out.println("Selected Item  = " + newSelected);
//             }
//         });
//	}

	
	
	public void fillSplitVerticalPanel(){
		  jSplitVerticalPanel = new JSplitPane(JSplitPane.VERTICAL_SPLIT, 
                  jRightUpPanel, colorPanel);
          jSplitVerticalPanel.setOneTouchExpandable(true);
          jSplitVerticalPanel.setDividerLocation(330);
	}
	
	public void fillSplitHorizontalPanel(){
		 jSplitHorizontalPanel = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, treeview, jSplitVerticalPanel);
	           jSplitHorizontalPanel.setOneTouchExpandable(true);
	           jSplitHorizontalPanel.setDividerLocation(600);
	}
	
	public void fillFrame(){
	    frame.getContentPane().add(jSplitHorizontalPanel);     
       	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			frame.pack();
			frame.setVisible(true);
	}
	
	
	
	
    /**
     * @param argv the command line arguments
     * @throws Exception 
     */
    public static void main(String[] argv) throws Exception {
   
    //    StartUp start = new StartUp();
    //    start.init();
       
         
    }

     
    
}